#!/usr/bin/env python
# coding: utf-8

# ## 必要条件

# In[37]:


import requests
import pandas as pd
chikei_key = '8971d403bd33010f7b5a707cfbdc1d77'


# ## 地理编码

# In[41]:


def geocode(key,address,city=None,batch="false",sig=None,output="json",callback=None):
    geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": chikei_key,
        "address": address,
        "city": city,
        "batch": batch,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(geocode_url, params=params) # 请求方式为get
    return r.json()


# ## 逆地理编码

# In[44]:


def re_geocode(key,location,poitype=None,radius=1000,extensions="base",batch="false",roadlevel=1,sig=None,output="json",callback=None,homeorcorp=0):
    re_geocode_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {"key": chikei_key,
              "location": location,
              "poitype": poitype,
              "radius": radius,
              "extensions": extensions,
              "batch": batch,
              "roadlevel":roadlevel,
              "sig": sig,
              "output": output,
              "callback": callback,
              "homeorcorp": homeorcorp
             }
    r = requests.get(re_geocode_url, params=params)
    return r.json()


# ## 路径规划

# ### 步行规划

# In[46]:


def walking(key,origin,destination,sig=None,output="json",callback=None):
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key": chikei_key,
        "origin": origin,
        "destination": destination,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(walking_url, params=params)
    return r.json()


# ### 公交规划

# In[48]:


def busing(key,origin,destination,city,cityd=None,extensions="base",strategy=0,nightflag=0,date=None,time=None,sig=None,output="json",callback=None):
    busing_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key": chikei_key,
        "origin": origin,
        "destination": destination,
        "city": city,
        "cityd": cityd,
        "extensions": extensions,
        "strategy": strategy,
        "nightflag": nightflag,
        "date": date,
        "time": time,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(busing_url, params=params)
    return r.json()


# ### 驾车规划

# In[49]:


def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=0,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation="false",nosteps=0,sig=None,output="json",callback=None,extensions="base"):
    driving_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key": chikei_key,
        "origin": origin,
        "destination": destination,
        "originid": originid,
        "destinationid": destinationid,
        "origintype": origintype,
        "destinationtype": destinationtype,
        "strategy": strategy,
        "waypoints": waypoints,
        "avoidpolygons": avoidpolygons,
        "avoidroad": avoidroad,
        "province": province,
        "number": number,
        "cartype": cartype,
        "ferry": ferry,
        "roadaggregation": roadaggregation,
        "nosteps": nosteps,
        "sig": sig,
        "output": output,
        "callback": callback,
        "extensions": extensions
    }
    r = requests.get(driving_url, params=params)
    return r.json()


# ### 骑行规划

# In[50]:


def bicycling(key,origin,destination):
    bicycling_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key": chikei_key,
        "origin": origin,
        "destination": destination,
    }
    r = requests.get(bicycling_url, params=params)
    return r.json()


# ### 货车规划

# In[54]:


def truck(key,origin, destination, size, originid=None, destinationid=None, origintype=None, destinationtype=None, diu=None, strategy=None, waypoints=None, height=None, width=None, load=None, weight=None, axis=None, province=None, number=None, cartype=None, avoidpolygons=None, showpolyline=None, nosteps=None):
    url = 'https://restapi.amap.com/v4/direction/truck?parameters'
    params = {
        'key': chikei_key,
        'origin': origin,
        'destination':destination,
        'size': size,
        'originid':originid,
        'destinationid':destinationid,
        'origintype':origintype,
        'destinationtype':destinationtype,
        'diu': diu,
        'strategy':strategy,
        'waypoints': waypoints,
        'height': height,
        "weight": weight,
        'width': width,
        'load': load,
        'axis': axis, 
        'province':province,
        'number':number,
        'cartype':cartype,
        'avoidpolygons':avoidpolygons,
        'showpolyline': showpolyline,
        'nosteps': nosteps       
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ### 物流距离测量矩阵

# In[55]:


def distance(origins, destination, type=None, sig=None, output=None, callback=None):
    url = 'https://restapi.amap.com/v3/distance?parameters'
    params = {
        'key': chikei_key,
        'origins': origins,
        'destination': destination,
        'type': type,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## 行政区域查询

# In[56]:


def district(key,keywords,subdistric,page=1,offset=20,extensions="base",district_filter="adcode",callback=None,output="json"):
    district_url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key": chikei_key,
        "keywords": keywords,
        "subdistric": subdistric,
        "page": page,
        "offset": offset,
        "extensions": extensions,
        "filter": district_filter,
        "callback": callback,
        "output": output
    }
    r = requests.get(district_url, params=params)
    return r.json()


# ## 搜索POI

# ### 关键字搜索

# In[58]:


def poi_keywords(key,keywords,types=None,city=None,citylimit="false",children=0,offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    poi_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key": chikei_key,
        "keywords": keywords,
        "types": types,
        "city": city,
        "citylimit": citylimit,
        "children": children,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(poi_url, params)
    return r.json()


# ### 周边搜索

# In[61]:


def poi_around(key,location,keywords=None,types="050000|070000|120000",city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    poi_around_url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key": chikei_key,
        "location": location,
        "keywords": keywords,
        "types": types,
        "city": city,
        "radius": radius,
        "sortrule": sortrule,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(poi_around_url, params)
    return r.json()


# ### 多边形搜索

# In[62]:


def poi_polygon(key,polygon,keywords=None,types="120000|150000",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    poi_polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
        "key": chikei_key,
        "polygon": polygon,
        "keywords": keywords,
        "types": types,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(poi_polygon_url, params)
    return r.json()


# ### ID查询

# In[64]:


def poi_id(key,interest_id,sig=None,output="json",callback=None):
    poi_id_url = "https://restapi.amap.com/v3/place/detail?parameters"
    params = {
        "key": chikei_key,
        "id": interest_id,
        "sig": sig,
        "output": output,
        "callback": callback}
    r = requests.get(poi_id_url, params)
    results = r.json()
    return results


# ## IP定位

# In[65]:


def ip(key,ip,sig=None,output="json"):
    ip_url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key": chikei_key,
        "ip": ip,
        "sig": sig,
        "output": output}
    r = requests.get(ip_url, params=params)
    return r.json()


# ## 静态地图

# In[66]:


#定义函数staticmap
from PIL import Image
from io import BytesIO
def static_map(key,location,zoom=15,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    static_map_url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": chikei_key,
        "location": location,
        "zoom": zoom,
        "size": size,
        "scale": scale,
        "markers": markers,
        "labels": labels,
        "paths": paths,
        "traffic": traffic,
        "sig": sig
    }
    r = requests.get(static_map_url, params)
    data = Image.open(BytesIO(r.content))
    return data
    


# ## 坐标转换

# In[67]:


def convert(key,location,coordsys="autonavi",sig=None,output="json"):
    convert_url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key": chikei_key,
        "locations": location,
        "coordsys": coordsys,
        "sig": sig,
        "output": output}
    r = requests.get(convert_url, params)
    return r.json()


# ## 天气查询

# In[68]:


def weather(key,city,extension="all",output="json"):
    weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key": chikei_key,
        "city": city,
        "extensions":extension,
        "output": output
    }
    r = requests.get(weather_url, params)
    return r.json()


# ## 输入提示

# In[69]:


def input_tips(key,keyword,poi_type=None,location=None,city=None,citylimit="false",datatype="all",sig=None,output="json",callback=None):
    input_tips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key": chikei_key,
        "keywords": keyword,
        "type": poi_type,
        "location": location,
        "city": city,
        "citylimit": citylimit,
        "datatype": datatype,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(input_tips_url, params)
    return r.json()


# In[ ]:




